package com.oneandseven.dajia.utils;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;

public class FileConverter {

    public static File convertMultipartFileToFile(MultipartFile multipartFile) throws IOException {
        if (multipartFile == null) {
            return null;
        }
        if (multipartFile.getOriginalFilename() == null) {
            return null;
        }
        File file = new File(multipartFile.getOriginalFilename());
        try (OutputStream os = Files.newOutputStream(file.toPath())) {
            os.write(multipartFile.getBytes());
        }
        return file;
    }
}